
--- 可以获得的牌名
---@param player Player
local function getXunBaoNams(player)
  local names = {"ex_nihilo", "snatch", "indulgence", "peach"}
  local extra = player:getTableMark("emo__xunbao_extra")
  return table.connect(names, extra)
end

local skel = fk.CreateSkill {
  name = "emo__xunbao",
  dynamic_desc = function (self, player, lang)
    local names = getXunBaoNams(player)
    return "emo__xunbao_dyn:" .. table.concat(table.map(names, function(name) return "【".. Fk:translate(name, lang).."】" end ), "/")
  end,
}

Fk:loadTranslationTable{
  ["emo__xunbao"] = "寻宝",
  [":emo__xunbao"] = "出牌阶段限一次，你可以弃置任意张两两点数之差大于3的手牌，亮出牌堆顶X张牌并获得其中的【无中生有】/【顺手牵羊】/【乐不思蜀】/【桃】，其余牌置入弃牌堆，若你未获得牌，则增加一种可获得的非基本牌名（X为你弃置牌点数之和）。",

  [":emo__xunbao_dyn"] = "出牌阶段限一次，你可以弃置任意张两两点数之差大于3的手牌，亮出牌堆顶X张牌并获得其中的{1}，其余牌置入弃牌堆，若你未获得牌，则增加一种可获得的牌名（X为你弃置牌点数之和）。",

  ["#emo__xunbao-prompt"] = "寻宝：弃置任意张两两点数之差大于3的手牌，翻开牌堆等量的牌！",
  ["#emo__xunbao-choice"] = "寻宝：选择增加一种你可以获得的牌名",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#emo__xunbao-prompt",
  times = function (self, player)
    return 1 - player:usedSkillTimes(self.name, Player.HistoryPhase)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  min_card_num = 1,
  card_filter = function (self, player, to_select, selected)
    if player:prohibitDiscard(Fk:getCardById(to_select)) or not table.contains(player:getCardIds("h"), to_select) then return false end
    return #selected == 0 or table.every(selected, function(id)
      return (math.abs(Fk:getCardById(to_select).number - Fk:getCardById(id).number)) > 3
    end)
  end,
  target_num = 0,
  on_use = function(self, room, effect)
    local player = effect.from
    local names = getXunBaoNams(player)
    local x = 0
    table.forEach(effect.cards, function(id)
      x = x + Fk:getCardById(id).number
    end)
    room:throwCard(effect.cards, self.name, player, player)
    if player.dead or x == 0 then return end
    local cards = room:getNCards(x)
    room:moveCards({ ids = cards, toArea = Card.Processing, moveReason = fk.ReasonPut, skillName = self.name, proposer = player })
    local get = table.filter(cards, function (id)
      if room:getCardArea(id) ~= Card.Processing then return false end
      return table.contains(names, Fk:getCardById(id).name)
    end)
    if #get > 0 and not player.dead then
      room:obtainCard(player, get, true, fk.ReasonPrey, player, self.name)
    end
    room:cleanProcessingArea(cards, skel.name)
    if player.dead or #get > 0 then return end
    local all = Fk:getAllCardNames("tde")
    local choices = table.filter(all, function(name) return not table.contains(names, name) end)
    if #choices == 0 then return end
    local choice = room:askToChoice(player, {
      choices = choices, skill_name = skel.name, prompt = "#emo__xunbao-choice",
    })
    room:addTableMark(player, "emo__xunbao_extra", choice)
  end,
})

return skel
